home *** CD-ROM | disk | FTP | other *** search
/ Champak 109 / Vol 109.iso / games / bubble_t.swf / scripts / frame_1 / DoAction_2.as next >
Text File  |  2008-11-12  |  18KB  |  567 lines

  1. function pressedMouse()
  2. {
  3.    if(gUseKeyboard.val)
  4.    {
  5.       return undefined;
  6.    }
  7.    fireProjectile();
  8. }
  9. function fireProjectile()
  10. {
  11.    if(gMode != cNORMAL)
  12.    {
  13.       return undefined;
  14.    }
  15.    if(gNewBubbleMode == cMOVE)
  16.    {
  17.       return undefined;
  18.    }
  19.    _root.machine_main.gotoAndPlay("pump");
  20.    gFireSound.attachSound("fire_sound");
  21.    gFireSound.start();
  22.    var a = _root.gun.barrel.pAngle;
  23.    if(a == -1.5707963267948966)
  24.    {
  25.       a += 0.00001;
  26.    }
  27.    _root.projectile._x = _root.gun._x + gGunLength * Math.cos(a);
  28.    _root.projectile._y = _root.gun._y + gGunLength * Math.sin(a);
  29.    gBounceBack = false;
  30.    gTarget = getTarget(a);
  31.    _root.projectile.bubble.gotoAndStop(gCurrentType);
  32.    _root.projectile.bubble.graphic.bonus.gotoAndStop(gCurrentBonus);
  33.    gMode = cFIRE;
  34.    gFrameTimer = getTimer();
  35.    _root.new_bubble.bubble.gotoAndStop(gNextType);
  36.    _root.new_bubble.bubble.graphic.bonus.gotoAndStop(gNextBonus);
  37.    _root.new_bubble._x = gNewBubbleStartX;
  38.    _root.new_bubble._y = gNewBubbleStartY;
  39.    gNewBubbleMode = cMOVE;
  40.    _root.future_bubble.bubble.gotoAndStop(gFutureType);
  41.    _root.future_bubble.bubble.graphic.bonus.gotoAndStop(gFutureBonus);
  42.    _root.future_bubble.gotoAndPlay("animate");
  43. }
  44. function getTarget(vAngle)
  45. {
  46.    gProjectileAngle = vAngle;
  47.    var lProjectileLoc = {x:_root.projectile._x,y:_root.projectile._y};
  48.    gTilt = -1.5707963267948966 - vAngle;
  49.    gTilt = normalizeAngle(gTilt);
  50.    var lOriginRot = rotate(gOrigin,lProjectileLoc,gTilt);
  51.    var ri0 = {x:gRdX,y:gRdY};
  52.    var ri1 = rotate(ri0,{x:0,y:0},gTilt);
  53.    var ci0 = {x:gCdX,y:gCdY};
  54.    var ci1 = rotate(ci0,{x:0,y:0},gTilt);
  55.    gTarget = 0;
  56.    var lClosestDestY = -1000;
  57.    gSquashDist = 0;
  58.    var i = gNumRows - 1;
  59.    while(i >= 0)
  60.    {
  61.       if(gBubbleRow[i].length >= 1)
  62.       {
  63.          var lRowOrigin = {x:lOriginRot.x + i * ri1.x,y:lOriginRot.y + i * ri1.y};
  64.          if(ci1.x < 0)
  65.          {
  66.             var lSign = -1;
  67.          }
  68.          else
  69.          {
  70.             var lSign = 1;
  71.          }
  72.          var lFirstCol = Math.ceil((lProjectileLoc.x - lSign * gSpacing - lRowOrigin.x) / ci1.x);
  73.          var lLastCol = Math.floor((lProjectileLoc.x + lSign * gSpacing - lRowOrigin.x) / ci1.x);
  74.          var j = lFirstCol;
  75.          while(lLastCol >= j)
  76.          {
  77.             if(j >= 0)
  78.             {
  79.                var b = getBubbleAt(i,j);
  80.                if(b != 0 && b != -1)
  81.                {
  82.                   var lTargetX = lRowOrigin.x + j * ci1.x;
  83.                   var lTargetY = lRowOrigin.y + j * ci1.y;
  84.                   var dx = lProjectileLoc.x - lTargetX;
  85.                   var lMinDist = gSpacing / 2 + gMinSpacing;
  86.                   if(dx >= lMinDist)
  87.                   {
  88.                      var lSquashDist = 0;
  89.                   }
  90.                   else
  91.                   {
  92.                      var lSquashDist = Math.sqrt(lMinDist * lMinDist - dx * dx);
  93.                   }
  94.                   var lDestY = lTargetY + lSquashDist;
  95.                   if(lClosestDestY < lDestY)
  96.                   {
  97.                      lClosestDestY = lDestY;
  98.                      gTarget = b;
  99.                      gSquashDist = lSquashDist;
  100.                      gStartSquashDist = Math.sqrt(gSpacing * gSpacing - dx * dx);
  101.                      gSquashThresholdDist = lProjectileLoc.y - lTargetY - gStartSquashDist;
  102.                   }
  103.                }
  104.             }
  105.             j++;
  106.          }
  107.       }
  108.       i--;
  109.    }
  110.    if(gTarget)
  111.    {
  112.       gTravelDist = 0;
  113.       gThresholdDist = lProjectileLoc.y - lClosestDestY;
  114.       var xt1 = lProjectileLoc.x;
  115.       var yt1 = lClosestDestY;
  116.       gTangentPosition = rotate({x:xt1,y:yt1},lProjectileLoc,- gTilt);
  117.       var lTangentPos = pixelsToPos(gTarget._x + _root.bubble_MCA._x,gTarget._y + _root.bubble_MCA._y,gTangentPosition.x,gTangentPosition.y);
  118.       var lSnap = new Array();
  119.       var lStartPos = Math.ceil(lTangentPos);
  120.       var i = 0;
  121.       while(i < 5)
  122.       {
  123.          var cw = lStartPos + i;
  124.          if(5 < cw)
  125.          {
  126.             cw -= 6;
  127.          }
  128.          var lNeighborPos = getNeighborPos(gTarget,cw);
  129.          var lRow = lNeighborPos.r;
  130.          var lCol = lNeighborPos.c;
  131.          var lNeighbor = getBubbleAt(lRow,lCol);
  132.          if(lNeighbor == 0 && onStage(lRow,lCol))
  133.          {
  134.             lSnap.push(cw);
  135.          }
  136.          else if(lNeighbor != 0 && lNeighbor != -1)
  137.          {
  138.             break;
  139.          }
  140.          i++;
  141.       }
  142.       var lStartPos = Math.floor(lTangentPos);
  143.       var i = 0;
  144.       while(i < 5)
  145.       {
  146.          var ccw = lStartPos - i;
  147.          if(ccw < 0)
  148.          {
  149.             ccw += 6;
  150.          }
  151.          var lNeighborPos = getNeighborPos(gTarget,ccw);
  152.          var lRow = lNeighborPos.r;
  153.          var lCol = lNeighborPos.c;
  154.          var lNeighbor = getBubbleAt(lRow,lCol);
  155.          if(lNeighbor == 0 && onStage(lRow,lCol))
  156.          {
  157.             lSnap.push(ccw);
  158.          }
  159.          else if(lNeighbor != 0 && lNeighbor != -1)
  160.          {
  161.             break;
  162.          }
  163.          i++;
  164.       }
  165.       if(lSnap.length == 0)
  166.       {
  167.          return -1;
  168.       }
  169.       if(1 < lSnap.length)
  170.       {
  171.          lSnap = lSnap.sort(sortByPositionDist);
  172.       }
  173.       var p = lSnap[0];
  174.       gFinalPosition = getNeighborPos(gTarget,p);
  175.       var lTargetLoc = gridToPixels(gTarget.pRow,gTarget.pCol);
  176.       gSquashAngle = Math.atan2(lTargetLoc.y - gTangentPosition.y,lTargetLoc.x - gTangentPosition.x);
  177.       return gTarget;
  178.    }
  179.    return 0;
  180. }
  181. function normalizeAngle(vAngle)
  182. {
  183.    while(vAngle < -3.141592653589793)
  184.    {
  185.       vAngle += 6.283185307179586;
  186.    }
  187.    while(3.141592653589793 < vAngle)
  188.    {
  189.       vAngle -= 6.283185307179586;
  190.    }
  191.    return vAngle;
  192. }
  193. function rotate(p, po, vAngle)
  194. {
  195.    var x0 = p.x - po.x;
  196.    var y0 = p.y - po.y;
  197.    var x1 = x0 * Math.cos(vAngle) - y0 * Math.sin(vAngle);
  198.    var y1 = x0 * Math.sin(vAngle) + y0 * Math.cos(vAngle);
  199.    x1 += po.x;
  200.    y1 += po.y;
  201.    return {x:x1,y:y1};
  202. }
  203. function remapAngle(vAngle1, vAngle2)
  204. {
  205.    vAngle1 -= vAngle2;
  206.    vAngle1 = normalizeAngle(vAngle1);
  207.    return vAngle1;
  208. }
  209. function moveProjectile(vTime)
  210. {
  211.    if(gMode == cREMOVE)
  212.    {
  213.       if(0 < gGroup.length)
  214.       {
  215.          var b = gGroup.shift();
  216.          var r = b.pRow;
  217.          var c = b.pCol;
  218.          var lIndex = getOne(gBubbleList,b);
  219.          gBubbleList.splice(lIndex,1);
  220.          var lIndex = getOne(gBubbleRow[b.pRow],b);
  221.          gBubbleRow[b.pRow].splice(lIndex,1);
  222.          gBubbleCount[b.bubble._currentFrame]--;
  223.          var lBonus = b.bubble.graphic.bonus._currentFrame;
  224.          if(1 < lBonus)
  225.          {
  226.             b.swapDepths(gTopLevel++);
  227.             gBonus *= lBonus;
  228.             gBonusDelay = 5;
  229.          }
  230.          b.bubble.graphic.gotoAndPlay("pop");
  231.          gPopping++;
  232.          gBubble[r][c] = undefined;
  233.          gPopSound.attachSound("pop_sound");
  234.          gPopSound.start();
  235.          updateScore(gUnitScore);
  236.          gMatchScore += gUnitScore;
  237.          if(gUnitScore == 2)
  238.          {
  239.             gUnitScore = 3;
  240.          }
  241.          else if(gUnitScore == 3)
  242.          {
  243.             gUnitScore = 5;
  244.          }
  245.          else if(gUnitScore == 5)
  246.          {
  247.             gUnitScore = 10;
  248.          }
  249.          else
  250.          {
  251.             gUnitScore += 10;
  252.          }
  253.          if(gGroup.length == 0)
  254.          {
  255.             gPopDelay = 5;
  256.             gUnitScore = 20;
  257.          }
  258.       }
  259.       else if(0 < gStranded.length)
  260.       {
  261.          if(gPopDelay)
  262.          {
  263.             gPopDelay--;
  264.             return undefined;
  265.          }
  266.          var b = gStranded.shift();
  267.          var r = b.pRow;
  268.          var c = b.pCol;
  269.          var lIndex = getOne(gBubbleList,b);
  270.          gBubbleList.splice(lIndex,1);
  271.          var lIndex = getOne(gBubbleRow[b.pRow],b);
  272.          gBubbleRow[b.pRow].splice(lIndex,1);
  273.          gBubbleCount[b.bubble._currentFrame]--;
  274.          b.bubble.graphic.bonus.gotoAndStop(1);
  275.          b.bubble.graphic.gotoAndPlay("pop");
  276.          gPopping++;
  277.          gBubble[r][c] = undefined;
  278.          gPopSound.attachSound("pop_sound");
  279.          gPopSound.start();
  280.          updateScore(gUnitScore);
  281.          gMatchScore += gUnitScore;
  282.          gUnitScore += 10;
  283.       }
  284.       else if(!gPopping)
  285.       {
  286.          if(1 < gBonus || gBonusDelay)
  287.          {
  288.             if(gBonusDelay)
  289.             {
  290.                gBonusDelay--;
  291.                return undefined;
  292.             }
  293.             if(1 >= gBonus)
  294.             {
  295.                gBonusDelay = 0;
  296.                return undefined;
  297.             }
  298.             gBonusDelay = 5;
  299.             updateScore(gMatchScore * (gBonus - 1));
  300.             gBonus = 1;
  301.             gBonusSound.attachSound("bonus_sound");
  302.             gBonusSound.start(0,3);
  303.          }
  304.          else if(gBubbleList.length == 0)
  305.          {
  306.             levelOver();
  307.          }
  308.          else
  309.          {
  310.             gMode = cNORMAL;
  311.          }
  312.       }
  313.       return undefined;
  314.    }
  315.    if(gMode == cCONTACT)
  316.    {
  317.       var r = gFinalPosition.r;
  318.       var c = gFinalPosition.c;
  319.       finishProjectile(r,c);
  320.       return undefined;
  321.    }
  322.    var lDist = (vTime - gFrameTimer) * gProjectileSpeed / 1000;
  323.    if(gDebug)
  324.    {
  325.       lDist = 125 * gProjectileSpeed / 1000;
  326.    }
  327.    var x0 = _root.projectile._x;
  328.    var y0 = _root.projectile._y;
  329.    var x = x0 + lDist * Math.cos(gProjectileAngle);
  330.    var y = y0 + lDist * Math.sin(gProjectileAngle);
  331.    gTravelDist += lDist;
  332.    if(gTarget)
  333.    {
  334.       _root.projectile._xscale = 100;
  335.       _root.projectile._yscale = 100;
  336.       if(gTravelDist >= gThresholdDist)
  337.       {
  338.          gMode = cCONTACT;
  339.          _root.projectile._x = gTangentPosition.x;
  340.          _root.projectile._y = gTangentPosition.y;
  341.          _root.projectile._rotation = gSquashAngle * 180 / 3.141592653589793 - 90;
  342.          _root.projectile._yscale = (1 - gMaxSquash) * 100;
  343.          _root.projectile.bubble._rotation = (- gSquashAngle) * 180 / 3.141592653589793 + 90;
  344.          return undefined;
  345.       }
  346.       if(gSquashThresholdDist < gTravelDist)
  347.       {
  348.          var lObjDist = dist(x,y,gTarget._x + gOrigin.x,gTarget._y + gOrigin.y);
  349.          var lScale = 100 * (lObjDist - gSpacing / 2) / (gSpacing / 2);
  350.          _root.projectile._rotation = gSquashAngle * 180 / 3.141592653589793 - 90;
  351.          _root.projectile._yscale = - lScale;
  352.          _root.projectile.bubble._rotation = (- gSquashAngle) * 180 / 3.141592653589793 - 90;
  353.          _root.projectile.bubble.graphic.bonus._xscale = lScale;
  354.          _root.projectile.bubble.graphic.bonus._yscale = lScale;
  355.       }
  356.    }
  357.    else
  358.    {
  359.       if(_root.wall_bottom._y + gSpacing / 2 < y)
  360.       {
  361.          gCurrentType = gNextType;
  362.          gCurrentBonus = gNextBonus;
  363.          gNextType = gFutureType;
  364.          gNextBonus = gFutureBonus;
  365.          gFutureType = randomType();
  366.          gFutureBonus = chooseBonus();
  367.          if(gNewBubbleMode == cMOVE)
  368.          {
  369.             _root.projectile._y = -200;
  370.             gProjectileWaiting = true;
  371.          }
  372.          else
  373.          {
  374.             _root.projectile._x = _root.gun._x;
  375.             _root.projectile._y = _root.gun._y;
  376.             gProjectileWaiting = false;
  377.          }
  378.          _root.projectile.bubble.gotoAndStop(gCurrentType);
  379.          _root.projectile._xscale = 100;
  380.          _root.projectile._yscale = 100;
  381.          _root.projectile._rotation = 0;
  382.          _root.projectile.bubble._rotation = 0;
  383.          gAttachSound.attachSound("lost_bubble_sound");
  384.          gAttachSound.start();
  385.          gMode = cNORMAL;
  386.          updateScore(-10);
  387.          return undefined;
  388.       }
  389.       if(x < _root.wall_left._x + gMinSpacing)
  390.       {
  391.          var lScale = (_root.wall_left._x + gMinSpacing - x0) / (x - x0);
  392.          x = x0 + lDist * Math.cos(gProjectileAngle) * lScale;
  393.          y = y0 + lDist * Math.sin(gProjectileAngle) * lScale;
  394.          gOldProjectileAngle = gProjectileAngle;
  395.          gProjectileAngle = reflect(gProjectileAngle,1.5707963267948966);
  396.          _root.projectile._x = x;
  397.          _root.projectile._y = y;
  398.          _root.projectile._xscale = (1 - gMaxSquash) * 100;
  399.          gTarget = getTarget(gProjectileAngle);
  400.          if(gTarget == -1)
  401.          {
  402.             gProjectileAngle = normalizeAngle(gOldProjectileAngle + 3.141592653589793);
  403.             gTarget = 0;
  404.          }
  405.          gBounceSound.attachSound("bounce_sound");
  406.          gBounceSound.start();
  407.       }
  408.       else if(_root.wall_right._x - gMinSpacing < x)
  409.       {
  410.          var lScale = (_root.wall_right._x - gMinSpacing - x0) / (x - x0);
  411.          x = x0 + lDist * Math.cos(gProjectileAngle) * lScale;
  412.          y = y0 + lDist * Math.sin(gProjectileAngle) * lScale;
  413.          gOldProjectileAngle = gProjectileAngle;
  414.          gProjectileAngle = reflect(gProjectileAngle,1.5707963267948966);
  415.          _root.projectile._x = x;
  416.          _root.projectile._y = y;
  417.          _root.projectile._xscale = (1 - gMaxSquash) * 100;
  418.          gTarget = getTarget(gProjectileAngle);
  419.          if(gTarget == -1)
  420.          {
  421.             gProjectileAngle = normalizeAngle(gOldProjectileAngle + 3.141592653589793);
  422.             gTarget = 0;
  423.          }
  424.          gBounceSound.attachSound("bounce_sound");
  425.          gBounceSound.start();
  426.       }
  427.       else if(y < _root.ceiling._y + gMinSpacing)
  428.       {
  429.          var lScale = (_root.ceiling._y + gMinSpacing - y0) / (y - y0);
  430.          x = x0 + lDist * Math.cos(gProjectileAngle) * lScale;
  431.          y = y0 + lDist * Math.sin(gProjectileAngle) * lScale;
  432.          gOldProjectileAngle = gProjectileAngle;
  433.          gProjectileAngle = reflect(gProjectileAngle,0);
  434.          _root.projectile._x = x;
  435.          _root.projectile._y = y;
  436.          _root.projectile._yscale = (1 - gMaxSquash) * 100;
  437.          if(gSticky.val)
  438.          {
  439.             var lCol = (x - _root.bubble_MCA._x) / gSpacing;
  440.             var lClosestCol = Math.floor(lCol + 0.5);
  441.             var b = getBubbleAt(0,lClosestCol);
  442.             if(b == 0)
  443.             {
  444.                gMode = cCONTACT;
  445.                gFinalPosition = {r:0,c:lClosestCol};
  446.                return undefined;
  447.             }
  448.             if(lCol < lClosestCol)
  449.             {
  450.                var lNextCol = lClosestCol - 1;
  451.             }
  452.             else
  453.             {
  454.                var lNextCol = lClosestCol + 1;
  455.             }
  456.             var b = getBubbleAt(0,lNextCol);
  457.             if(b == 0)
  458.             {
  459.                gMode = cCONTACT;
  460.                gFinalPosition = {r:0,c:lNextCol};
  461.                return undefined;
  462.             }
  463.             gTarget = getTarget(gProjectileAngle);
  464.             if(gTarget == -1)
  465.             {
  466.                gProjectileAngle = normalizeAngle(gOldProjectileAngle + 3.141592653589793);
  467.                gTarget = getTarget(gProjectileAngle);
  468.             }
  469.             gBounceSound.attachSound("bounce_sound");
  470.             gBounceSound.start();
  471.          }
  472.          else
  473.          {
  474.             gTarget = getTarget(gProjectileAngle);
  475.             if(gTarget == -1)
  476.             {
  477.                gProjectileAngle = normalizeAngle(gOldProjectileAngle + 3.141592653589793);
  478.                gTarget = 0;
  479.             }
  480.             gBounceSound.attachSound("bounce_sound");
  481.             gBounceSound.start();
  482.          }
  483.       }
  484.       else if(x < _root.wall_left._x + gSpacing / 2)
  485.       {
  486.          var lScale = 100 * (x - _root.wall_left._x) * 2 / gSpacing;
  487.          _root.projectile._xscale = lScale;
  488.       }
  489.       else if(_root.wall_right._x - gSpacing / 2 < x)
  490.       {
  491.          var lScale = 100 * (_root.wall_right._x - x) * 2 / gSpacing;
  492.          _root.projectile._xscale = lScale;
  493.       }
  494.       else if(y < _root.ceiling._y + gSpacing / 2)
  495.       {
  496.          var lScale = 100 * (y - _root.ceiling._y) * 2 / gSpacing;
  497.          _root.projectile._yscale = lScale;
  498.       }
  499.       else
  500.       {
  501.          _root.projectile._xscale = 100;
  502.          _root.projectile._yscale = 100;
  503.       }
  504.    }
  505.    _root.projectile._x = x;
  506.    _root.projectile._y = y;
  507. }
  508. function finishProjectile(r, c)
  509. {
  510.    var lName = "bubble" + r + "_" + c;
  511.    _root.bubble_MCA.attachMovie("bubble",lName,gBubbleLevel++);
  512.    var lBubble = _root.bubble_MCA[lName];
  513.    lBubble.pRow = r;
  514.    lBubble.pCol = c;
  515.    if(gDebug)
  516.    {
  517.       lBubble.bubble.position_display = "" + r + "," + c;
  518.    }
  519.    placeBubble(lBubble);
  520.    lBubble.bubble.gotoAndStop(gCurrentType);
  521.    gBubbleCount[gCurrentType]++;
  522.    lBubble.bubble.graphic.bonus.gotoAndStop(gCurrentBonus);
  523.    gBubble[r][c] = lBubble;
  524.    gBubbleList.push(lBubble);
  525.    gBubbleRow[r].push(lBubble);
  526.    gCurrentType = gNextType;
  527.    gCurrentBonus = gNextBonus;
  528.    gNextType = gFutureType;
  529.    gNextBonus = gFutureBonus;
  530.    gFutureType = randomType();
  531.    gFutureBonus = chooseBonus();
  532.    if(gDebug)
  533.    {
  534.       gNextType = 1;
  535.    }
  536.    _root.projectile.bubble.gotoAndStop(gCurrentType);
  537.    _root.projectile._xscale = 100;
  538.    _root.projectile._yscale = 100;
  539.    _root.projectile.bubble._yscale = 100;
  540.    _root.projectile._rotation = 0;
  541.    _root.projectile.bubble._rotation = 0;
  542.    _root.projectile.bubble.graphic.bonus._xscale = 100;
  543.    _root.projectile.bubble.graphic.bonus._yscale = 100;
  544.    if(gNewBubbleMode == cMOVE)
  545.    {
  546.       _root.projectile._y = -200;
  547.       gProjectileWaiting = true;
  548.    }
  549.    else
  550.    {
  551.       _root.projectile._x = _root.gun._x;
  552.       _root.projectile._y = _root.gun._y;
  553.       gProjectileWaiting = false;
  554.    }
  555.    gAttachSound.attachSound("attach_sound");
  556.    gAttachSound.start();
  557.    if(checkDie())
  558.    {
  559.       return undefined;
  560.    }
  561.    var lMatch = checkMatch(lBubble);
  562.    if(!lMatch)
  563.    {
  564.       lBubble.bubble.graphic.bonus.gotoAndStop(1);
  565.    }
  566. }
  567.